package com.smile.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smile.commons.page.MyBatisPlusPageConverter;
import com.smile.commons.page.R;
import com.smile.model.dto.request.EditTaskStatusRequestDTO;
import com.smile.model.dto.request.GetTaskDetailRequestDTO;
import com.smile.model.dto.request.GetTaskListRequestDTO;
import com.smile.model.response.response.GetTaskDetailResponseDTO;
import com.smile.model.response.response.GetTaskListResponseDTO;
import com.smile.model.request.EditTaskStatusRequest;
import com.smile.model.request.GetTaskDetailRequest;
import com.smile.model.request.GetTaskListRequest;
import com.smile.model.response.GetTaskDetailResponse;
import com.smile.model.response.GetTaskListResponse;
import com.smile.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private TaskService taskService;

    @PostMapping("/getTaskList")
    public R<Page<GetTaskListResponse>> getTaskList(@RequestBody GetTaskListRequest request) {
        GetTaskListRequestDTO getTaskListRequestDTO = BeanUtil.copyProperties(request, GetTaskListRequestDTO.class);
        Page<GetTaskListResponseDTO> page = taskService.getTaskList(getTaskListRequestDTO);

        return R.ok(MyBatisPlusPageConverter.convertPage(page, GetTaskListResponse.class));
    }

    @PostMapping("/getTaskDetail")
    public R<GetTaskDetailResponse> getTaskDetail(@RequestBody GetTaskDetailRequest request) {
        GetTaskDetailRequestDTO getTaskDetailRequestDTO = BeanUtil.copyProperties(request, GetTaskDetailRequestDTO.class);
        GetTaskDetailResponseDTO getTaskDetailResponseDTO = taskService.getTaskDetail(getTaskDetailRequestDTO);
        return R.ok(BeanUtil.copyProperties(getTaskDetailResponseDTO, GetTaskDetailResponse.class));
    }

    @PostMapping("/editTaskStatus")
    public R<Void> editTaskStatus(@RequestBody EditTaskStatusRequest editTaskStatusRequest) {
        EditTaskStatusRequestDTO editTaskStatusRequestDTO = BeanUtil.copyProperties(editTaskStatusRequest, EditTaskStatusRequestDTO.class);
        Boolean result = taskService.editTaskStatus(editTaskStatusRequestDTO);
        if (result) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

}
